/*
function addCart(itemNo,specNo,qty,categoryPath,type,webpath) {
  var winName = "_parent" ;
  var features = "top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes" ;
  var url = webpath+"/shopping/cart.do?" ;
  url = url + "itemNo=" + encodeURIComponent(itemNo);
  url = url + "&specNo=" + encodeURIComponent(specNo);
  url = url + "&qty=" + encodeURIComponent(qty);
  url = url + "&categoryPath=" + encodeURIComponent(categoryPath);
  url = url + "&type=" + encodeURIComponent(type);
  url = url + "&webpath=" + encodeURIComponent(webpath);
  window.open(url,winName,features);
}
*/
/**
 * {
 * itemNo: "00000001",
 * specNo: "specNo",
 * qty: "123",
 * categoryPath: "1/2/3",
 * type: "type",
 * webpath: "webpath",
 * presents: [{itemNo:"00000002"}, {itemNo:"00000003"}],
 * addOns: [{itemNo:"00000004"}, {itemNo: "00000005"}]
 * }
 * 
 */
function addCart(data){
	var winName = "_parent" ;
	  var features = "top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes" ;
	  //alert(data['webpath']);
	 // var url = data['webpath']+"/shopping/cart.do?" ;
	  var url = data['webpath'] ;
	  url = url + "itemNo=" + encodeURIComponent(data['itemNo']);
	  url = url + "&specNo=" + encodeURIComponent(data['specNo']);
	  url = url + "&qty=" + encodeURIComponent(data['qty']);
	  url = url + "&categoryPath=" + encodeURIComponent(data['categoryPath']);
	  url = url + "&type=" + encodeURIComponent(data['type']);
	  url = url + "&presents=" + jsonToString(data['presents']);
	  //url = url + "&presents=" + "[{test:\"123\"},{test1:\"234\"}]";
	  url = url + "&addOns=" + jsonToString(data['addOns']);
	  if ( hasTlProduct == "true" ){
		  $.post("/ecmall/shopping/checkTlpCart.do",{
			  itemNo:encodeURIComponent(data['itemNo']),
			  specNo:encodeURIComponent(data['specNo']),
			  qty:encodeURIComponent(data['qty']),
			  categoryPath:encodeURIComponent(data['categoryPath']),
			  type:encodeURIComponent(data['type']),
			  presents:jsonToString(data['presents']),
			  addOns:jsonToString(data['addOns']),
			  rs:'product'},
					function callback(result){	
					if(result == '2'){	
						jsonToAddCart(data);
					}else if(result == '1' || result == '3'){
						content='<div class="alert-caution"><p>每档限时秒杀活动，<b class="txt-rd">同一帐号限购一项商品。</b><br>您可参加下一档秒杀活动喔，谢谢您！</p><div align="center"><a href="javascript:closeDialog();" class="btn-mid">关闭</a></div></div>';
						openDialog('活动信息', content,370,'auto');	
					}},"json");
		  
	  }else{
		  if ( data['type'] != "6" ){
		  $.post("/ecmall/shopping/checkCart.do",{
			  itemNo:encodeURIComponent(data['itemNo']),
			  specNo:encodeURIComponent(data['specNo']),
			  qty:encodeURIComponent(data['qty']),
			  categoryPath:encodeURIComponent(data['categoryPath']),
			  type:encodeURIComponent(data['type']),
			  presents:jsonToString(data['presents']),
			  addOns:jsonToString(data['addOns'])},
					function callback(result){	
					if(result == 'false'){	
						jsonToAddCart(data);
					}else{
						functionName = 'location.href=\'../shopping/cart.do\'';
						content='<div class="pdnm-alert"><p>此商品属性已存在于购物车中！</p><div align="center"><a href="javascript:'+functionName+';closeDialog();" class="btn-mid" onfocus="blur()">查看购物车</a><a href="javascript:closeDialog();" class="btn-mid">确定</a></div></div>';
						openDialog('选购商品', content,430,180);
					}},"json");
			}else{
				jsonToAddCart(data);
			}
	  }
	
		//window.open(url,winName,features);
	//  return false;
	  
	  //alert(url);
	 // alert('presents1:'+data['presents'][0]['itemNo'])
	//  alert(jsonToString(data['presents']));
	  
	
}
function jsonToAddCart(data){
	$.post("/ecmall/shopping/addCart.do",{
		  itemNo:encodeURIComponent(data['itemNo']),
		  specNo:encodeURIComponent(data['specNo']),
		  qty:encodeURIComponent(data['qty']),
		  categoryPath:encodeURIComponent(data['categoryPath']),
		  type:encodeURIComponent(data['type']),
		  presents:jsonToString(data['presents']),
		  addOns:jsonToString(data['addOns'])},
				function callback(addCartResult){
				if(addCartResult == 'success'){	
					$("#cartItems").text(getCookie("_1.100.CI").split("=")[1]);
					functionName = 'location.href=\'../shopping/cart.do\'';
			    	content='<div class="alert-ok"><p>已成功加入购物车！</p><div align="center"><a href="javascript:'+functionName+';closeDialog();" class="btn-mid" onfocus="blur()">查看购物车</a><a href="javascript:closeDialog();" class="btn-mid">确定</a></div></div>';				
			    	openDialog('加入购物车', content,430,180);
				}else{
					functionName = 'location.href=\'../shopping/cart.do\'';
					content='<div class="pdnm-alert"><p>加入购物车失败！</p><div align="center"><a href="javascript:'+functionName+';closeDialog();" class="btn-mid" onfocus="blur()">查看购物车</a><a href="javascript:closeDialog();" class="btn-mid">确定</a></div></div>';					
					openDialog('加入购物车', content,430,180);
				}},"json");
}
function jsonToString(obj){  
         var THIS = this;   
         switch(typeof(obj)){  
             case 'string':  
                 return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';  
             case 'array':  
                 return '[' + obj.map(THIS.jsonToString).join(',') + ']';  
             case 'object':  
                  if(obj instanceof Array){  
                     var strArr = [];  
                     var len = obj.length;  
                     for(var i=0; i<len; i++){  
                         strArr.push(THIS.jsonToString(obj[i]));  
                     }  
                     return '[' + strArr.join(',') + ']';  
                 }else if(obj==null){  
                     return 'null';  
   
                 }else{  
                     var string = [];  
                     for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property]));  
                     return '{' + string.join(',') + '}';  
                 }  
             case 'number':  
                 return obj;  
             case false:  
                 return obj;  
         }  
}
function addCartForm(formObj) { 
  addCart(formObj.itemNo.value,formObj.specNo.value,formObj.qty.value,formObj.categoryPath.value,formObj.type.value,formObj.webpath.value);
}

function getCookie(name) {
    var theCookies = document.cookie.split(/[; ]+/);
    for (var i = 0 ; i < theCookies.length; i++) {
        var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
        if (aName == name) {
            return theCookies[i];
        }
    }
}
